package com.demo.testpackage;

import com.demo.entity.Fish;
import com.demo.entity.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId("1");
        student.setName("张三");
        Fish fish = new Fish();
        fish.setId("2");
        fish.setName("白鲢");
        System.out.println("转json前：" + student);
        ObjectMapper objectMapper = new ObjectMapper();
        String studentJson = null;
        try {
            studentJson = objectMapper.writeValueAsString(student);
        } catch (JsonProcessingException e) {
            log.error("对象【{}】转json失败", student, e);
        }
        System.out.println("转json后：" + studentJson);
        Fish stu=null;
        try {
             stu = objectMapper.readValue(studentJson, Fish.class);
        } catch (JsonProcessingException e) {
            log.error("解析json【{}】字符串失败。",studentJson,e);
        }
        System.out.println("json字符串解析后："+stu);
    }
}
